package com.wiley.loginPO;

import java.net.MalformedURLException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import com.wiley.publicMethod.BasePage;
import com.wiley.publicMethod.LoggerControler;

import io.appium.java_client.android.AndroidDriver;

public class LoginWithQQPage extends BasePage {

	/**
	 * 页面元素
	 */
	//我的
	public static final By mine_item_loc = By.id(BaseId + "mine_item");

	//点击登录按钮
	public static final By login_tv_loc = By.id(BaseId + "login_tv");

	//社交账号：QQ
	public static final By login_with_qq_loc = By.id(BaseId + "login_with_qq");
	
	//切换账号
	public static final By exchange_account_loc = By.name("切换帐号");
	
	//QQ 账号(0) 密码(1)
	public static final By qq_loc = By.className("android.widget.EditText");
	
	//登录
	public static final By qq_login_loc = By.xpath("//android.widget.Button[@text='登录']");
	public static final By qq_submit_loc = By.name("登 录");
	
	//用户昵称
	public static final By nick_tv_loc = By.id(BaseId + "nick_tv");

	/**
	 * 操作方法
	 */
	//点击我的
	public void clickMineItem(){
		this.clickEle(mine_item_loc);
	}

	//登录按钮
	public void clickLoginButton(){
		this.clickEle(login_tv_loc);
	}

	//QQ账号登录
	public void clickLoginWithQQ(){
		this.clickEle(login_with_qq_loc);
	}
	
	public void loginWithQQ(){
		this.clickEle(exchange_account_loc);
		this.sendKeysByIndex(qq_loc, 0, "2815001745");
		this.sendKeysByIndex(qq_loc, 1, "fanguoweivip123");
		this.clickEle(qq_submit_loc);
		Assert.assertTrue(getUserName().equals("天空sky6"));
	}
	
	//用户昵称
	public String getUserName(){
		WebElement ele = this.findEle(nick_tv_loc);
		return ele.getText();
	}
	
	/**
	 * 业务封装
	 * @throws MalformedURLException 
	 */
	//登录
	public void LoginWithQQ(Class name) throws MalformedURLException  {
		LoggerControler log = this.MyLogger(name);
		AndroidDriver driver = super.SetDriver();
		this.clickMineItem();
		this.clickLoginButton();
		this.clickLoginWithQQ();
		if(super.isElementPresent(qq_login_loc)){ //判断是否有登录按钮
			this.clickEle(qq_login_loc);
			Assert.assertTrue(getUserName().equals("天空sky6"));
			log.info("该case测试通过！");
		}else{
			this.loginWithQQ();
			log.info("该case测试通过！");
		}
	}
}
